/*
 * Created with Sublime Text 3.
 * license: http://www.lovewebgames.com/jsmodule/index.html
 * User: 田想兵
 * Date: 2015-06-11
 * Time: 16:27:55
 * Contact: 55342775@qq.com
 */
(function(root, factory) {
	//amd
	if (typeof define === 'function' && define.amd) {
		define(['$', 'query'], factory);
	} else if (typeof exports === 'object') { //umd
		module.exports = factory();
	} else {
		root.Paging = factory(window.Zepto || window.jQuery || $, Query);
	}
})(this, function($, Query) {
	$.fn.Paging = function(settings) {
		var arr = [];
		$(this).each(function() {
			var options = $.extend({
				target: $(this)
			}, settings);
			var lz = new Paging();
			lz.init(options);
			arr.push(lz);
		});
		return arr;
	};

	function Paging() {
		var rnd = Math.random().toString().replace('.', '');
		this.id = 'Paging_' + rnd;
	}
	Paging.prototype = {
		init: function(settings) {
			this.settings = $.extend({
				callback: null,
				pagesize: 7,
				current: 1,
				prevTpl: "上一页",
				nextTpl: "下一页",
				firstTpl: "首页",
				lastTpl: "末页",
				ellipseTpl: "...",
				toolbar: false,
				hash:true,
				pageSizeList: [7, 10, 15, 20]
			}, settings);
			this.target = $(this.settings.target);
			this.container = $('<div id="' + this.id + '" class="ui-paging-container"/>');
			this.target.append(this.container);
			this.render(this.settings);
			this.format();
			this.bindEvent();
		},
		render: function(ops) {
			this.count = ops.count || this.settings.count;
			this.pagesize = ops.pagesize || this.settings.pagesize;
			this.current = ops.current || this.settings.current;
			this.pagecount = Math.ceil(this.count / this.pagesize);
			this.format();
		},
		bindEvent: function() {
			var _this = this;
			this.container.on('click', 'li.js-page-action,li.ui-pager', function(e) {
				if ($(this).hasClass('ui-pager-disabled') || $(this).hasClass('focus')) {
					return false;
				}
				if ($(this).hasClass('js-page-action')) {
					if ($(this).hasClass('js-page-first')) {
						_this.current = 1;
					}
					if ($(this).hasClass('js-page-prev')) {
						_this.current = Math.max(1, _this.current - 1);
					}
					if ($(this).hasClass('js-page-next')) {
						_this.current = Math.min(_this.pagecount, _this.current + 1);
					}
					if ($(this).hasClass('js-page-last')) {
						_this.current = _this.pagecount;
					}
				} else if ($(this).data('page')) {
					_this.current = parseInt($(this).data('page'));

                }
				_this.go();
			});
			/*
			$(window).on('hashchange',function(){
				var page=  parseInt(Query.getHash('page'));
				if(_this.current !=page){
					_this.go(page||1);
				}
			})
			 */
		},
		go: function(p) {
			var _this = this;
			this.current = p || this.current;
			this.current = Math.max(1, _this.current);
			this.current = Math.min(this.current, _this.pagecount);
			this.format();
			if(this.settings.hash){
				Query.setHash({
					page:this.current
				});
			}
			this.settings.callback && this.settings.callback(this.current, this.pagesize, this.pagecount);
		},
		changePagesize: function(ps) {
			this.render({
				pagesize: ps
			});
		},
		format: function() {
			var html = '<ul class="pagination pagination-sm m-t-none m-b-none clearfix">'
			html += '<li class="js-page-first js-page-action ui-pager clearfix" ><a href="javascript:;">' + this.settings.firstTpl + '</a></li>';
			html += '<li class="js-page-prev js-page-action ui-pager clearfix"><a href="javascript:;">' + this.settings.prevTpl + '</a></li>';
			if (this.pagecount > 6) {
				html += '<li data-page="1" class="ui-pager clearfix"><a href="javascript:;">1</a></li>';
				if (this.current <= 2) {
					html += '<li data-page="2" class="ui-pager clearfix"><a href="javascript:;">2</a></li>';
					html += '<li data-page="3" class="ui-pager clearfix"><a href="javascript:;">3</a></li>';
					html += '<li class="ui-paging-ellipse"><a href="javascript:;">' + this.settings.ellipseTpl + '</a></li>';
				} else
				if (this.current > 2 && this.current <= this.pagecount - 2) {
					html += '<li class="clearfix"><a href="javascript:;">' + this.settings.ellipseTpl + '</a></li>';
					html += '<li data-page="' + (this.current - 1) + '" class="ui-pager clearfix"><a href="javascript:;">' + (this.current - 1) + '</a></li>';
					html += '<li data-page="' + this.current + '" class="ui-pager clearfix"><a href="javascript:;">' + this.current + '</a></li>';
					html += '<li data-page="' + (this.current + 1) + '" class="ui-pager clearfix"><a href="javascript:;">' + (this.current + 1) + '</a></li>';
					html += '<li class="ui-paging-ellipse" class="ui-pager clearfix"><a href="javascript:;">' + this.settings.ellipseTpl + '</a></li>';
				} else {
					html += '<li class="ui-paging-ellipse clearfix" ><a href="javascript:;">' + this.settings.ellipseTpl + '</a></li>';
					for (var i = this.pagecount - 2; i < this.pagecount; i++) {
						html += '<li data-page="' + i + '" class="ui-pager clearfix"><a href="javascript:;">' + i + '</a></li>'
					}
				}
				html += '<li data-page="' + this.pagecount + '" class="ui-pager clearfix"><a href="javascript:;">' + this.pagecount + '</a></li>';
			} else {
				for (var i = 1; i <= this.pagecount; i++) {
					html += '<li data-page="' + i + '" class="ui-pager clearfix"><a href="javascript:;">' + i + '</a></li>'
				}
			}
			html += '<li class="js-page-next js-page-action ui-pager clearfix"><a href="javascript:;">' + this.settings.nextTpl + '</a></li>';
			html += '<li class="js-page-last js-page-action ui-pager clearfix"><a href="javascript:;">' + this.settings.lastTpl + '</a></li>';
			html += '</ul>';
			this.container.html(html);
			if (this.current == 1) {
				$('.js-page-prev', this.container).addClass('ui-pager-disabled');
				$('.js-page-first', this.container).addClass('ui-pager-disabled');
			}
			if (this.current == this.pagecount) {
				$('.js-page-next', this.container).addClass('ui-pager-disabled');
				$('.js-page-last', this.container).addClass('ui-pager-disabled');
			}
			this.container.find('li[data-page="' + this.current + '"]').addClass('focus').siblings().removeClass('focus');
			if (this.settings.toolbar) {
				this.bindToolbar();
			}

            this.selAdapterView();

		},
		bindToolbar: function() {
			var _this = this;
			var html = $('<li class="ui-paging-toolbar clearfix"><select class="ui-select-pagesize"></select><input type="text" class="ui-paging-count"/><a href="javascript:;" class="jump">跳转</a></li>');
			var sel = $('.ui-select-pagesize', html);
			var str = '';
			for (var i = 0, l = this.settings.pageSizeList.length; i < l; i++) {
				str += '<option value="' + this.settings.pageSizeList[i] + '">' + this.settings.pageSizeList[i] + '条/页</option>';
			}
			sel.html(str);
			sel.val(this.pagesize);
			$('input', html).val(this.current);
			$('input', html).click(function() {
				$(this).select();
			}).keydown(function(e) {
				if (e.keyCode == 13) {
					var current = parseInt($(this).val()) || 1;
					_this.go(current);
				}
			});
			$('a', html).click(function() {
				var current = parseInt($(this).prev().val()) || 1;
				_this.go(current);
			});
			sel.change(function() {
				_this.changePagesize($(this).val());
			});
			this.container.children('ul').append(html);
		},
        selAdapterView:function() {
        var data1=[
                {
                    "id":"1",
                    "name":"王以南",
                    "sex":"女",
                    "date":"1993-5-16",
                    "type":"小儿睡眠障碍",
                    "money":"200",
                    "time":"2017-3-4",
                    "Jid":"3"
                },
                {
                    "id":"2",
                    "name":"王以南",
                    "sex":"女",
                    "date":"1993-5-16",
                    "type":"小儿生长发育",
                    "money":"200",
                    "time":"2017-3-4",
                    "Jid":"3"
                },
                {
                    "id":"3",
                    "name":"王以南",
                    "sex":"女",
                    "date":"1993-5-16",
                    "type":"小儿癫痫",
                    "money":"200",
                    "time":"2017-3-4",
                    "Jid":"3"
                },
                {
                    "id":"4",
                    "name":"王以南",
                    "sex":"女",
                    "date":"1993-5-16",
                    "type":"小儿癫痫",
                    "money":"200",
                    "time":"2017-3-4",
                    "Jid":"3"
                },
                {
                    "id":"5",
                    "name":"王以南",
                    "sex":"女",
                    "date":"1993-5-16",
                    "type":"白血病",
                    "money":"200",
                    "time":"2017-3-4",
                    "Jid":"3"
                },
                {
                    "id":"6",
                    "name":"王以南",
                    "sex":"女",
                    "date":"1993-5-16",
                    "type":"小儿癫痫",
                    "money":"200",
                    "time":"2017-3-4",
                    "Jid":"3"
                },
                {
                    "id":"7",
                    "name":"王以南",
                    "sex":"女",
                    "date":"1993-5-16",
                    "type":"小儿生长发育",
                    "money":"200",
                    "time":"2017-3-4",
                    "Jid":"3"
                },
                {
                    "id":"8",
                    "name":"王以南",
                    "sex":"女",
                    "date":"1993-5-16",
                    "type":"小儿睡眠障碍",
                    "money":"200",
                    "time":"2017-3-4",
                    "Jid":"3"
                },
                {
                    "id":"9",
                    "name":"王以南",
                    "sex":"女",
                    "date":"1993-5-16",
                    "type":"白血病",
                    "money":"200",
                    "time":"2017-3-4",
                    "Jid":"3"
                }

            ];

        var step=this.pagesize;
        var data=data1.slice((this.current-1)*step,this.current*step);

        $(".table").empty();
        var s_title="<tr><th>ID</th><th>患者姓名</th><th>性别</th><th>出生日期</th><th>交易类型</th><th>虚拟币金额</th><th>时间</th><th>监护人ID</th></tr>";

        $(".table").append(s_title);
        var typeArr=[];
        var all=0;
        var typeoneArr=[];
        var typetwoArr=[];
        var typethreeArr=[];
        var typefourArr=[];
            var type1=0;
            var type2=0;
            var type3=0;
            var type4=0;
        $.each(data,function (index,ele) {
            var html="";
            html+="<tr><td>"+ele.id+"</td><td>"+ele.name+"</td><td>"+ele.sex+"</td><td>"+ele.date+"</td><td>"+ele.type+"</td><td>"+ele.money+"</td><td>"+ele.time+"</td><td>"+ele.Jid+"</td></tr>";
            $(".table").append(html);

            // 计算健康币总额并存储
            var money=Number(ele.money);
            all+=money;
            sessionStorage.setItem("all",all);

            // 统计交易类型去重并存储
            typeArr.push(ele.type);
            // console.log(typeArr);
            // sessionStorage.setItem("typeArr",typeArr);
            var arrType=[];
            quchong(typeArr,arrType);
            // console.log(arrType);
            sessionStorage.setItem("arrType",arrType);

			function quchong(arr,arrNew) {
                for(var i= 0;i<arr.length;i++){
                    if(arrNew.indexOf(arr[i])<0){
                        arrNew.push(arr[i]);
                    }
                }
                return arrNew;
            }


             // 统计各个类型所消费的健康币总额
			var arr=[];
			arr.push(ele.type);
			arr.push(ele.money);
			// console.log(arr);

/*
            if(ele.type=="小儿癫痫"){
				type1+=Number(ele.money);
				console.log(type1);

            }
           */

			/*$.each(arrType,function (index,type) {
				if(ele.type="小儿癫痫"){
					a.push(ele.money);
					console.log(a);
				}
            })*/


        });

			/* $.ajax({
			 type:"post",
			 url:"",
			 dataType:"json",
			 data:{
			 page:
			 pageCount:7
			 }
			 success:function (data) {
			 $(".table").empty();
			 var s_title="<tr><th>ID</th><th>患者姓名</th><th>性别</th><th>出生日期</th><th>交易类型</th><th>虚拟币金额</th><th>时间</th><th>监护人ID</th></tr>";

			 $(".table").append(s_title);
			 $.each(data,function (index,ele) {
			 var html="";
			 html+="<tr><td>"+ele.id+"</td><td>"+ele.name+"</td><td>"+ele.sex+"</td><td>"+ele.date+"</td><td>"+ele.type+"</td><td>"+ele.money+"</td><td>"+ele.time+"</td><td>"+ele.Jid+"</td></tr>";
			 $(".table").append(html);
			 });
			 }
			 })*/

    }
	};
	return Paging;
});